package com.huaxin.hxmodulesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaxin.hxmodulesystem.dto.ResponseCode;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.AddOperationLogReqDTO;
import com.huaxin.hxmodulesystem.mapper.OperationLogMapper;
import com.huaxin.hxmodulesystem.mapping.OperationLogMapping;
import com.huaxin.hxmodulesystem.models.OperationLog;
import com.huaxin.hxmodulesystem.service.OperationLogService;
import com.huaxin.hxmodulesystem.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {
    private final OperationLogMapping operationLogMapping;

    private final ApplicationContext applicationContext;

    /**
     * 新增操作日志
     *
     * @param in
     * @return
     */
    @Override
    public boolean addOperationLog(AddOperationLogReqDTO in) {
        if (in == null) {
            return false;
        }
        if (StringUtils.isBlank(in.getUserId())) {
            return false;
        }
        if (StringUtils.isBlank(in.getOperationType())) {
            return false;
        }
        if (StringUtils.isBlank(in.getDescription())) {
            return false;
        }
        OperationLog operationLog = operationLogMapping.toEntity(in);
        return this.save(operationLog);
    }

    /**
     * 获取操作日志列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<OperationLog> getOperationLogList(Integer pageNum, Integer pageSize, String userId, Integer operationType, String startTime, String endTime) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        Page<OperationLog> page = new Page<>(pageNum, pageSize);
        QueryWrapper<OperationLog> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(userId)) {
            wrapper.eq("user_id", userId);
        }
        if (operationType != null) {
            wrapper.eq("operation_type", operationType);
        }
        if (StringUtils.isNotBlank(startTime)) {
            wrapper.ge("operation_time", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            wrapper.le("operation_time", endTime);
        }
        return this.baseMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    /**
     * 获取操作日志详情
     *
     * @param id
     * @return
     */
    @Override
    public OperationLog getOperationLogDetail(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        return this.getById(id);
    }

    /**
     * 删除操作日志
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult<String> deleteOperationLog(String id) {
        if (StringUtils.isBlank(id)) {
            return ResponseResult.parameterError(null, "操作日志ID不能为空");
        }
        if (applicationContext.getBean(RoleService.class).removeById(id)) {
            return ResponseResult.ok("删除操作日志成功！");
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "删除操作日志失败！", null);
        }
    }

    /**
     * 清理操作日志
     */
    @Override
    public ResponseResult<String> clearOperationLogs() {
        //删除3个月之前的数据
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<OperationLog>().lt("created_at", LocalDateTime.now().minusMonths(3));
        if (applicationContext.getBean(OperationLogService.class).remove(queryWrapper)) {
            return ResponseResult.ok("清理操作日志成功！");
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "清理操作日志失败，请稍后再试！", null);
        }
    }
}